<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2018/11/20
 * Time: 16:48
 */
namespace common\sources\write\taobao;

use common\sources\write\taobao\ITaoBaoAccess;
use common\models\craftsman\TaoBao;
use Yii;

class TaoBaoAccess implements ITaoBaoAccess
{
    public function addTaoBao($param)
    {
        return Yii::$app->db->createCommand()->insert('taobao', [
            'tbname' =>  $param['tbname'],
            'buy_id' => $param['buy_id'],
            'sex' => $param['sex'],
            'zfbname'=>$param['zfbname'],
            'zfbage' => $param['zfbage'],
            'takename' => $param['takename'],
            'takephone' => $param['takephone'],
            'pro_name' =>$param['pro_name'],
            'pro_code' => $param['pro_code'],
            'city_name' => $param['city_name'],
            'city_code' => $param['city_code'],
            'area_name' => $param['area_name'],
            'area_code' => $param['area_code'],
            'addr' => $param['addr'],
            'zfbname_img' => $param['zfbname_img'],
            'ip_img' => $param['ip_img'],
            'tqz_img' => $param['tqz_img'],
            'addr' => $param['addr'],
            'zfbhb_img' => $param['zfbhb_img'],
            'zfbname_img' => $param['zfbname_img'],
            'create_time' => time(),
            'audit_status' => 1,
        ])->execute();
//        $taobao = new TaoBao();
//        $taobao->tbname = $param['tbname'];
//        $taobao->sex = $param['sex'];
//        $taobao->takename = $param['takename'];
//        $taobao->takephone = $param['takephone'];
//        $taobao->area = $param['area'];
//        $taobao->addr = $param['addr'];
//        $taobao->zfbsex_img = $param['zfbsex_img'];
//        $taobao->ip_img = $param['ip_img'];
//        $taobao->tqz_img = $param['tqz_img'];
//        $taobao->takeaddr = $param['takeaddr'];
//        $taobao->takeaddrs = $param['takeaddrs'];
//        $taobao->zfbname = $param['zfbname'];
//        $taobao->habit_ids = $param['habit_ids'];
//        $taobao->zfbname_img = $param['zfbname_img'];
//        $taobao->sphabit_img1 = $param['sphabit_img1'];
//        $taobao->sphabit_img2 = $param['sphabit_img2'];
//        $taobao->id_img = $param['id_img'];
//        $taobao->zfbnum = $param['zfbnum'];
//        $taobao->create_time = time();
//        $taobao->audit_status = 1;
//        return $taobao->save() ?  $taobao : FALSE;
    }
    //修改买号会员名称
    public function modifyTbName($data)
    {
        $sql = 'UPDATE ' . TaoBao::tableName() . ' SET tbname = :tbname,update_time=:update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':tbname' => $data['tbname'],
                ':update_time' => time(),
                ':id' => $data['t_id']
            ])->execute();
    }
    //删除买号
    public function delMember($tid)
    {
        $taobao = TaoBao::findOne(['id' => $tid]);
        if ($taobao) {
            if ($taobao->delete()) {
                return true;
            }
        }
        return false;
    }
    //修改收件人地址信息
    public function modifyAreaAddr($data)
    {
        $sql = 'UPDATE ' . TaoBao::tableName() . ' SET takename = :takename,pro_name = :pro_name,pro_code = :pro_code,city_name = :city_name,city_code = :city_code,area_name = :area_name,area_code = :area_code,takephone = :takephone,addr = :addr,update_time=:update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':takename' => $data['takename'],
                ':pro_name' => $data['pro_name'],
                ':pro_code' => $data['pro_code'],
                ':city_name' => $data['city_name'],
                ':city_code' => $data['city_code'],
                ':area_name' => $data['area_name'],
                ':area_code' => $data['area_code'],
                ':takephone' => $data['takephone'],
                ':addr' => $data['addr'],
                ':update_time' => time(),
                ':id' => $data['t_id']
            ])->execute();

    }
    //修改买号状态
    public function verifyTaoBao($data)
    {
        $taoBao = TaoBao::findOne(['id' => $data['t_id']]);
        if ($taoBao) {
            $taoBao->reason = $data['reason'];
            $taoBao->audit_status = $data['audit_status'];
            $taoBao->update_time = time();
            $taoBao->auditUser = $data['auditUser'];
            $taoBao->audit_operator = $data['audit_operator'];
            $taoBao->audit_time = time();
            if ($taoBao->save()) {
                return true;
            }
        }
        return false;
    }
    //绑定淘宝显示隐藏
    public function display($data)
    {
        $sql = 'UPDATE ' . TaoBao::tableName() . ' SET is_display = :is_display,update_time=:update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':is_display' => $data['display'],
                ':update_time' => time(),
                ':id' => $data['o_id']
            ])->execute();
    }

    //修改买号性别
    public function modifySex($data)
    {
        $taobo = TaoBao::findOne(['id' => $data['o_id']]);
        if ($taobo) {
            $taobo->sex = $data['sex'];
            if ($taobo->save()) {
                return true;
            }
        }
        return false;
    }

    //修改可接任务数
    public function modifyAccessTaskNum($tId,$num)
    {
        $taobo = TaoBao::findOne(['id' => $tId]);
        if ($taobo) {
            $taobo->access_task_num = $num;
            if ($taobo->save()) {
                return true;
            }
        }
        return false;
    }

    //冻结/解冻买号
    public function freeze($data)
    {
        $taobo = TaoBao::findOne(['id' => $data['t_id']]);
        if ($taobo) {
            $taobo->freeze = $data['pass'];
            $taobo->freeze_reason = $data['reason'];
            $taobo->freezeUser = $data['freezeUser'];
            $taobo->freeze_time = time();
            if ($taobo->save()) {
                return true;
            }
        }
        return false;
    }

	/**
	 * thawFreeze 解冻冻结
	 * @param $data
	 * @author 中崋 (1291713293@qq.com)
	 * @date   2019/5/9
	 * @return int
	 * @throws \yii\db\Exception
	 */
	public function thawFreeze($data) {
		$sql = 'UPDATE ' . TaoBao::tableName() . ' SET  freezeUser = :freezeUser , freeze = :freeze ,freeze_time = :freeze_time ,thaw_time = :thaw_time ,freeze_reason = :freeze_reason,update_time=:update_time WHERE id = :id ';
		return Yii::$app->db->createCommand($sql)
			->bindValues([
				':freeze'        => $data['freeze'],
				':freeze_reason' => $data['freeze_reason'],
				':freeze_time'   => $data['freeze_time'],
				':thaw_time'     => $data['thaw_time'],
				':freezeUser'    => $data['freezeUser'],
				':update_time'   => time(),
				':id'            => $data['id']
			])->execute();
	}

    //冻结/解冻买号
    public function batchFreeze($data)
    {
        $taobo = TaoBao::find()
            ->where(['buy_id' => $data['buy_id'],'tbname' => $data['tbname']])
            ->one();
        if ($taobo) {
            $taobo->freeze = $data['pass'];
            $taobo->freeze_reason = $data['reason'];
            $taobo->freezeUser = $data['freezeUser'];
            if ($taobo->save()) {
                return true;
            }
        }
        return false;
    }

    //备注
    public function Remark($data)
    {
        $taobo = TaoBao::findOne(['id' => $data['t_id']]);
        if ($taobo) {
            $taobo->remark = $data['remark'];
            $taobo->remarkUser = $data['remarkUser'];
            if ($taobo->save()) {
                return true;
            }
        }
        return false;
    }
}